<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController {
	function showlist(){
		if(empty($_POST)){
		$info = D('Article')->select();
		$this->assign('show',$info);
		$list = D('Artype')->select();
		$this->assign('list',$list);
		$this->display();
		}else{
			$where['typeid'] = $_GET['id'];
			$info = D('Article')->where($where)->select();
			$this->assign('show',$info);
			$list = D('Artype')->select();
			$this->assign('list',$list);
			$this->display();
			}
		}
	function add(){
		if(empty($_POST)){
			$info = D('Artype')->where('type = 1')->select();
			$this->assign('show',$info);
			$class = D('Artclass')->select();
			$this->assign('list',$class );
			$this->display();
			}else{
				$types = implode(',',$_POST['status']);
				$_POST['status'] = $types;
				if($_FILES['pic']['error']<4){
					$cfg = array(
						'rootPath' => './Public/Uploads/', //保存路径
					);
					$up = new \Think\Upload($cfg);
					$z = $up -> uploadOne($_FILES['pic']);					
					$_POST['pic'] = $z['savepath'].$z['savename'];
					}
				$_POST['creattime'] = date("Y-m-d",time());	
				$add = D('Article')->add($_POST);
				if($add){
					$this->success('添加成功');
					}else{
						$this->error('添加失败');
						}
				}
			}
	function update(){
		$where['id'] = $_GET['id'];
		if(empty($_POST)){
			$class = D('Artclass')->select();
			$this->assign('class',$class );
			$info = D('Artype')->select();
			$this->assign('show',$info);
			$list = D('Article')->where($where)->find();
			$have = $list['status'];
            $have = explode(',',$have);
			$this->assign('have',$have);
			$this->assign('list',$list);
			$this->display();
		}else{
			$types = implode(',',$_POST['status']);
			$_POST['status'] = $types;
			if($_FILES['pic']['error']<4){
					$cfg = array(
						'rootPath' => './Public/Uploads/', //保存路径
					);
					$up = new \Think\Upload($cfg);
					$z = $up -> uploadOne($_FILES['pic']);					
					$_POST['pic'] = $z['savepath'].$z['savename'];
					}
			$_POST['creattime'] = date("Y-m-d",time());	
			$info = D('Article')->where($where)->save($_POST);
			if($info){
				$this->success('数据修改成功');
				}else{
					$this->error('数据修改失败');
					}
			}
		}
		
	function delete(){
		$where['id']=$_GET['id'];
		$dele = D('Article')->where($where)->delete();
			if($dele){
				$this->success('删除成功');
				}else{
					$this->error('删除失败');
			}
		}
	function addgoods(){
		if(empty($_POST)){
			$info = D('Artype')->where('type = 2')->select();
			$this->assign('show',$info);
			$class = D('Artclass')->select();
			$this->assign('list',$class );
			$this->display();
			}else{
				if(!empty($_POST['status'])){
				$types = implode(',',$_POST['status']);
				$_POST['status'] = $types;
				}else{
					$types = 0;
					}
				if($_FILES['goods_img']['error']<4){
					$cfg = array(
						'rootPath' => './Public/Uploads/', //保存路径
					);
					$up = new \Think\Upload($cfg);
					$z = $up -> uploadOne($_FILES['pic']);					
					$_POST['goods_img'] = $z['savepath'].$z['savename'];
					}
				$_POST['creattime'] = date("Y-m-d",time());	
				$add = D('Goods')->add($_POST);
				if($add){
					$this->success('添加成功');
					}else{
						$this->error('添加失败');
						}
				}
			}
	function deletegoods(){
		$where['id']=$_GET['id'];
		$dele = D('Goods')->where($where)->delete();
			if($dele){
				$this->success('删除成功');
				}else{
					$this->error('删除失败');
			}
		}
	function updategoods(){
		$where['id'] = $_GET['id'];
		if(empty($_POST)){
			$class = D('Artclass')->select();
			$this->assign('class',$class );
			$info = D('Artype')->where('type = 2')->select();
			$this->assign('show',$info);
			$list = D('Goods')->where($where)->find();
			$have = $list['status'];
            $have = explode(',',$have);
			$this->assign('have',$have);
			$this->assign('list',$list);
			$this->display();
		}else{
			$types = implode(',',$_POST['status']);
			$_POST['status'] = $types;
			if($_FILES['goods_img']['error']<4){
					$cfg = array(
						'rootPath' => './Public/Uploads/', //保存路径
					);
					$up = new \Think\Upload($cfg);
					$z = $up -> uploadOne($_FILES['pic']);					
					$_POST['goods_img'] = $z['savepath'].$z['savename'];
					}
			$_POST['creattime'] = date("Y-m-d",time());	
			$info = D('Goods')->where($where)->save($_POST);
			if($info){
				$this->success('数据修改成功');
				}else{
					$this->error('数据修改失败');
					}
			}
		}
}